package com.ssm.util;


import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.twelvemonkeys.lang.StringUtil;

/** 
 * 基于HandlerExceptionResolver接口的异常处理类 
 */  
public class MyExceptionHandler implements HandlerExceptionResolver
{

	  @Override
	    public ModelAndView resolveException(HttpServletRequest request,
	            HttpServletResponse response, Object object, Exception exception)
	    {
	        exception.printStackTrace();
	        //判断请求错误的异常来自  ajax还是传统http请求
	        String requestType = request.getHeader("X-Requested-With");
	        int excelFlag = 0;
	        if (!StringUtil.isEmpty(requestType) && requestType.equals("XMLHttpRequest"))
	        {
	            try
	            {
	                response.sendError(500);
	            }
	            catch(IOException e)
	            {
	                e.printStackTrace();
	            }
	        }
	        Map<String, Object> model = new HashMap<String, Object>();
	        BusinessException businessException = null;
	        //数组下标越界
	        if (exception instanceof ArrayIndexOutOfBoundsException)
	        {
	            businessException = new BusinessException(ErrorCode.ARRAY_INDEXOF_BOUNDSEXCEPTION.toString()); 
	        //
	        }else if (exception instanceof ArithmeticException)
	        {
	            businessException = new BusinessException(ErrorCode.ARITHMETICEXECPTION.toString());
	        }else if (exception instanceof ClassCastException)
	        {
	            businessException = new BusinessException(ErrorCode.CLASSCASTEXCEPTION.toString());
	        }else if (exception instanceof NegativeArraySizeException)
	        {
	            businessException = new BusinessException(ErrorCode.NEGATIVEARRAYEXCEPTION.toString());
	        }else if(exception instanceof  SecurityException)
	        {
	            businessException = new BusinessException(ErrorCode.SECTURITYEXCEPTION.toString());
	        }else if (exception instanceof EOFException)
	        {
	            businessException = new BusinessException(ErrorCode.EOFEXCEPTION.toString());
	        }else if (exception instanceof FileNotFoundException)
	        {
	            businessException = new BusinessException(ErrorCode.FILENOTFOUNDEXCEPTION.toString());
	        }else if(exception instanceof  NumberFormatException)
	        {
	            businessException = new BusinessException(ErrorCode.NUMBERFORMATEXCEPTION.toString());
	        }else if(exception instanceof SQLException)
	        {
	            businessException = new BusinessException(ErrorCode.SQLEXCEPTION.toString());
	        }else if (exception instanceof IOException)
	        {
	            businessException = new BusinessException(ErrorCode.IOEXCEPTION.toString());
	        }else if (exception instanceof NoSuchMethodException)
	        {
	            businessException = new BusinessException(ErrorCode.NOSUCHMETHODEXCEPTION.toString());
	        }else if (exception instanceof NullPointerException)
	        {
	            businessException = new BusinessException(ErrorCode.NULL_OBJ.toString());
	        }else if (exception instanceof BusinessException)
	        {
	            businessException = new BusinessException(exception.getMessage());
	        }
	        else if (exception instanceof InterruptedException)
	        {
	            businessException = new BusinessException(ErrorCode.INTERRUPTEDEXCEPTION.toString());
	        }
	        else if (exception instanceof RemoteException)
	        {
	            businessException = new BusinessException(ErrorCode.REMOTE_EXCEPTION.toString());
	        }
	        else if(exception instanceof  ExcelException)
	        {
	            businessException = new BusinessException("导入的Excel格式有误，请修改Excel并且重新导入！！！");
	            excelFlag = 1;
	        }
	        else {
	            businessException = new BusinessException(ErrorCode.UNKNOWN_ERROR.toString());
	        }
	        
	        
	        if (excelFlag == 1)
            {
	            businessException.setStackTrace(exception.getStackTrace());
	            model.put("excelException",businessException);
	            return new ModelAndView("excelException",model);
            }else {
                businessException.setStackTrace(exception.getStackTrace());
                model.put("exception",businessException);
                return new ModelAndView("exception",model);
            }
	      
	    }
}